package com.zlzlib.libscan;

import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.AsyncTask;


import java.lang.ref.WeakReference;


/**
 * Created by zlz on 2018/9/14.
 * 后台识别的类
 */
public class BaseProcessTask extends AsyncTask<Void, Void, ScanResult> {

    private Camera mCamera;
    //拍摄的数据流
    private byte[] mData;
    //竖屏还是横屏
    private boolean mIsPortrait;
    //选择的图片地址
    private String mPicturePath;
    //直接解析的图片
    private Bitmap mBitmap;
    //弱引用 如果BaseScanView 被释放  则可以被cg回收
    private WeakReference<BaseScanView> scanViewRef;


    public BaseProcessTask(Camera mCamera, BaseScanView view, byte[] mData, boolean mIsPortrait) {
        this.mCamera = mCamera;
        this.mData = mData;
        this.mIsPortrait = mIsPortrait;
        scanViewRef = new WeakReference<>(view);
    }

    public BaseProcessTask(BaseScanView view, String mPicturePath) {
        this.mPicturePath = mPicturePath;
        scanViewRef = new WeakReference<>(view);
    }

    public BaseProcessTask(BaseScanView view, Bitmap mBitmap) {
        this.mBitmap = mBitmap;
        scanViewRef = new WeakReference<>(view);
    }


    BaseProcessTask perform() {
        executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        return this;
    }

    void cancelTask() {
        if (getStatus() != Status.FINISHED) {
            cancel(true);
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        scanViewRef.clear();
        mBitmap = null;
        mData = null;
    }

    @Override
    protected ScanResult doInBackground(Void... voids) {
        BaseScanView scanView = scanViewRef.get();
        if (scanView == null) {
            return null;
        }
        if (mPicturePath != null) {
            return scanView.processBitmapData(Utils.getDecodeAbleBitmap(mPicturePath));
        } else if (mBitmap != null) {
            ScanResult result = scanView.processBitmapData(mBitmap);
            mBitmap = null;
            return result;
        } else {
            ScanResult scanResult = processData(scanView);
            return scanResult;
        }
    }

    @Override
    protected void onPostExecute(ScanResult scanResult) {
        BaseScanView scanView = scanViewRef.get();
        if (scanView == null) {
            return;
        }
        if (mPicturePath != null || mBitmap != null) {
            mBitmap = null;
            //图片识别结果
            scanView.onPostBitmapOrPicture(scanResult);
        } else {
            //扫描识别结果
            scanView.onPostData(scanResult);
        }
    }

    //优化扫描数据
    private ScanResult processData(BaseScanView scanView) {
        if (mData == null) {
            return null;
        }
        int width = 0;
        int height = 0;
        byte[] data = mData;
        try {
            Camera.Parameters parameters = mCamera.getParameters();
            Camera.Size size = parameters.getPreviewSize();
            width = size.width;
            height = size.height;
            //如果是竖屏  把数据转成横向
            if (mIsPortrait) {
                data = new byte[mData.length];
                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        data[x * height + height - y - 1] = mData[x + y * width];
                    }
                }
                int tmp = width;
                width = height;
                height = tmp;
            }
            //解析数据
            return scanView.processData(data, width, height, false);
        } catch (Exception e1) {
            e1.printStackTrace();
            try {
                if (width != 0 && height != 0) {
                    return scanView.processData(data, width, height, true);
                } else {
                    return null;
                }
            } catch (Exception e2) {
                e2.printStackTrace();
                return null;
            }
        }
    }
}
